import 'package:mvvm_provider/base/base_state.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_model_entity.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_recommend_entity.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_series_entity.dart';
import 'package:mvvm_provider/page/home/data/repository/home_repository.dart';
import 'package:mvvm_provider/page/home/states/home_detail_state.dart';
import '../../../base/base_view_model.dart';
import '../../../model/response_model.dart';

class HomeDetailViewModel extends BaseViewModel<HomeDetailState> {
  HomeDetailViewModel() : super(HomeDetailState());

  /// 改变间距和透明度
  void changeAppBarAlphaAndOffset(double alpha, double newOffset) {
    state.appBarAlpha = alpha;
    state.offset = newOffset;
    notifyListeners();
  }

  /// 请求全部数据
  getAllData() async {
    await Future.wait<dynamic>([getMainData(), getSeriesData(), getRecommendData()]).then((value) {
      if (value[0] == null || value[1] == null || value[2] == null) {
        state.netState = NetState.errorShowRefresh;
        notifyListeners();
        return;
      }
      state.mainModel = value[0];
      state.seriesList = value[1];
      state.recommendList = value[2];
      state.netState = NetState.dataSuccessState;
      notifyListeners();
    }).catchError((error) {
      state.netState = NetState.errorShowRefresh;
      notifyListeners();
    });
  }

  /// 请求主数据
  getMainData() async {
    ResponseModel responseModel = await HomeRepository.getMainData<CartoonModelData>();
    return responseModel.data;
  }

  /// 请求系列数据
  getSeriesData() async {
    ResponseModel responseModel = await HomeRepository.getSeriesData<CartoonSeriesData>();
    CartoonSeriesData? cartoonSeriesData = responseModel.data;
    return cartoonSeriesData?.seriesComics;
  }

  /// 请求推荐数据
  getRecommendData() async {
    ResponseModel responseModel = await HomeRepository.getRecommendData<CartoonRecommendData>();
    CartoonRecommendData? cartoonRecommendData = responseModel.data;
    return cartoonRecommendData?.infos;
  }
}
